//
//  CustomcollectionView.m
//  releaseInterface
//
//  Created by Gamefire on 16/4/14.
//  Copyright © 2016年 Gamefire. All rights reserved.
//

#import "CustomcollectionView.h"
#import "CustomCollectionViewCell.h"
#import "Header.h"

//#define  UIColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

#define kWindowWidth                        ([[UIScreen mainScreen] bounds].size.width)

#define kWindowHeight                       ([[UIScreen mainScreen] bounds].size.height)


@interface CustomcollectionView ()<CustomCollectionViewCellDelegate>
{
    NSMutableArray *mutableArray;
//    NSMutableArray *sourceMutableArray;
//    NSMutableArray *connectArray;
    BOOL change;
}

@end




@implementation CustomcollectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        mutableArray = [NSMutableArray new];
//        sourceMutableArray = [NSMutableArray new];
//        connectArray = [NSMutableArray new];
        
        change = NO;
        [self setBackgroundColor:uicolor(237, 237, 237)];
        _flowlayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_flowlayout];
        [_collectionView setDataSource:self];
        [_collectionView setDelegate:self];
        [_collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
        [_collectionView setBackgroundColor:uicolor(237, 237, 237)];
        UIPanGestureRecognizer *longGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
        [_collectionView addGestureRecognizer:longGesture];
        [self addSubview:_collectionView];
//        [_collectionView setBackgroundColor:[UIColor redColor]];
    }
    return self;
}
    
//- (void)setFrame:(CGRect)frame
//{
//    [_collectionView setFrame:frame];
//}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(biliwith(70.0), biliwith(70.0));
}



- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(7, 7, 7, 7);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 4.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 4.0;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return mutableArray.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"identifier";
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    [cell setMCustomCollectionViewCellDelegate:self];
    [cell setTag:indexPath.row];
    [cell loadImage:[UIImage imageWithData:[[mutableArray objectAtIndex:indexPath.row] valueForKey:@"postdata"]] withChange:change];
    return cell;
}


- (void)ChangeImage:(BOOL)canChange
{
    if (canChange)
    {
        [_collectionView setUserInteractionEnabled:YES];
        change = YES;
    }
    else
    {
        [_collectionView setUserInteractionEnabled:NO];
        change = NO;
    }
    
    [_collectionView reloadData];
}




- (void)handlelongGesture:(UIGestureRecognizer *)longGesture {
    //判断手势状态
    switch (longGesture.state) {
        case UIGestureRecognizerStateBegan:{
            //判断手势落点位置是否在路径上
            NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:[longGesture locationInView:_collectionView]];
            if (indexPath == nil) {
                break;
            }
            //在路径上则开始移动该路径上的cell
            [_collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
        }
            break;
        case UIGestureRecognizerStateChanged:
            //移动过程当中随时更新cell位置
            [_collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:_collectionView]];
            break;
        case UIGestureRecognizerStateEnded:
            //移动结束后关闭cell移动
            [_collectionView endInteractiveMovement];
            break;
        default:
            [_collectionView cancelInteractiveMovement];
            break;
    }
}




- (void)shanchu:(NSInteger)tag
{
    [mutableArray removeObjectAtIndex:tag];
//    [sourceMutableArray removeObjectAtIndex:tag];
//    [connectArray removeObjectAtIndex:tag];
    [_collectionView reloadData];
    [_MCustomcollectionViewDelegate changeArrayWithEdite:mutableArray];
}





#pragma mark----数据刷新
- (void)setDataArray:(NSArray *)dataArray
{
    [mutableArray addObjectsFromArray:dataArray];
    
    [_collectionView reloadData];
}

////设置原图片数据
//- (void)setSourceDataArrya:(NSArray *)sourceDataArrya
//{
//    [sourceMutableArray addObjectsFromArray:sourceDataArrya];
//}
//
////设置关联id
//- (void)setConnectIDArray:(NSMutableArray *)connectIDArray
//{
//    
//    [connectArray addObjectsFromArray:connectIDArray];
//}


- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}



- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath{
//    NSLog(@"---------%@", sourceMutableArray);
    
    [mutableArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    [_collectionView reloadData];
    [_MCustomcollectionViewDelegate changeArrayWithEdite:mutableArray];
}

@end
